package com.get;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.add.Post;
import com.jdbcUtil.JDBCUtil;

public class PostImpl {
	public ArrayList<Post> getBiList() {
		ArrayList<Post> ciList=new ArrayList<Post>();
		Connection Conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Conn = JDBCUtil.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 创建一个可以滚动的只读的SQL语句对象
		try {
			stmt = Conn.createStatement(
					java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
					java.sql.ResultSet.CONCUR_READ_ONLY);
		} catch (SQLException e) {
			e.printStackTrace();
		}// 准备SQL语句
		String sql="SELECT *  FROM post ORDER BY postid DESC LIMIT 0, 4;";
		// 执行SQL语句并获取结果集
		try {
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				Post post=new Post();
				post.setId(rs.getInt("postid"));
				post.setJobCount(rs.getInt("jobcount"));
				post.setAuthorId(rs.getInt("authorid"));
				post.setPlace(rs.getString("joboffer"));
				post.setDate("2016-05-24");
				ciList.add(post);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return ciList;
	}
	public Post getPost(int postid){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Post post=new Post();
		try{
			conn=JDBCUtil.getConnection();
			String sql="select * from post where postid="+postid+"";
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				post.setId(postid);
				post.setAuthorId(rs.getInt("authorid"));
				post.setDate(rs.getString("date"));
				post.setJobCount(rs.getInt("jobcount"));
				post.setPlace(rs.getString("joboffer"));
				return post;
			}
		}catch(Exception e){
			
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return null;
	}
}
